﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.IO;
using System.Data;
using System.Text;
using SimpleInterestBiz;
namespace InterestCalculation.Website_Project.Web_Method
{
    /// <summary>
    /// Summary description for WebMethodSaacCalculator
    /// </summary>
    public class WebMethodSaacCalculator : IHttpHandler
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string method = context.Request.QueryString["method"].ToString();
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            try
            {
                switch (method)
                {
                    case "SaacSchedule":
                        var jsonString = string.Empty;
                        JavaScriptSerializer jss = new JavaScriptSerializer();
                        context.Request.InputStream.Position = 0;
                        //using (var inputStream = new StreamReader(context.Request.InputStream))
                        //{
                        //    jsonString = inputStream.ReadToEnd();
                        //}
                        jsonString = context.Request.QueryString["insertstring"];    
                        var propObj = jss.Deserialize<SaacScheduleProperties>(jsonString);

                        context.Response.Write(GetJson(SaacScheduleHandler.CalculateLoan(propObj)));
                        break;
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.ToString());
            }
        }


        /// <summary>
        /// Make json string from dataset for all table 
        /// </summary>
        /// <param name="ds">Pass whole dataset</param>
        /// <returns>json String</returns>
        public static string GetJson(DataSet ds)
        {
            StringBuilder sbjson = new StringBuilder();
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Dictionary<string, object> dt = new Dictionary<string, object>();
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                DataTable dts = ds.Tables[i];
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                foreach (DataRow dr in dts.Rows)
                {
                    Dictionary<string, object> data = new Dictionary<string, object>();
                    foreach (DataColumn col in dts.Columns)
                    {
                        data.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(data);
                }
                dt.Add(ds.Tables[i].TableName, rows);
            }
            sbjson.Append(jss.Serialize(dt));
            return sbjson.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}